<?php

namespace App\Http\Controllers\Service;

use App\Entity\Member;
use App\Entity\TempEmail;
use App\Entity\TempPhone;
use App\Http\Controllers\Controller;
use App\Models\M3Result;
use App\Tool\SMS\SendTemplateSMS;
use App\Tool\Validate\ValidateCode;
use Illuminate\Http\Request;

class ValidateController extends Controller
{


    public function create(Request $request)
    {
        $validateCode = new ValidateCode();
        $request->session()->put('validate_code',$validateCode->getCode());

        return $validateCode->doimg();
    }

    public function sendSMS(Request $request)
    {
        $m3Result = new M3Result();
        $code = $this->createCode();
        $deadline = 5;
        $phone = $request->input('phone', '');

        if ($phone == '') {
            $m3Result->status = 1;
            $m3Result->message = '手机号不能为空';
            return $m3Result->toJson();
        }

        if (strlen($phone) != 11 || $phone[0] != 1) {
            $m3Result->status = 2;
            $m3Result->message = '手机号格式不对';
            return $m3Result->toJson();
        }

        $sendTemplateSMS = new SendTemplateSMS();
        $m3Result = $sendTemplateSMS->sendTemplateSMS($phone, array($code, $deadline), 1);

        if ($m3Result->status == 0) {

            $tempPhone = TempPhone::query()->where('phone', $phone)->first();

            if ($tempPhone == null) {
                $tempPhone = new TempPhone();
            }


            $tempPhone->phone = $phone;
            $tempPhone->code = $code;
            date_default_timezone_set("PRC");
            $tempPhone->deadline = date('Y-m-d H:i:s',time() + $deadline * 60 );
            $tempPhone->save();
        }


        return $m3Result->toJson();
    }

    public function validate_email(Request $request)
    {
        $m3Result = new M3Result();

        $email = $request->input('email','');
        $code = $request->input('code','');


        $tempEmail = TempEmail::query()->where('email', $email)->first();

        if ($tempEmail == null) {
            $m3Result -> status = 1;
            $m3Result -> message = '注册失败';
            return $m3Result->toJson();
        }

        if ($tempEmail->code == $code) {
            if( strtotime($tempEmail->deadline)  > time() ) {
                $member = Member::query()->where('email', $email)->first();
                $member->active = 1;
                $member->save();

                $m3Result->status = 0;
                $m3Result->message = '注册成功';
            } else {
                $m3Result->status = 1;
                $m3Result->message = '邮箱验证已过期!';
            }
        } else {
           $m3Result->status = 2;
           $m3Result->message = '邮箱验证码错误';
        }


        return $m3Result->toJson();

    }

    private function createCode(): string
    {
        $charset = '123456789';//随机因子
        $code = '';
        $_len = strlen($charset) - 1;
        for ($i = 0;$i < 4;++$i) {
            $code .= $charset[mt_rand(0, $_len)];
        }

        return $code;
    }
}
